//
//  YFCommonViewController.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/8.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFCommonViewController.h"


#define kSectionHeadViewHeight 7.0f
#define kRowHeight 50.0f  // 默认cell的高度

@interface YFCommonViewController ()

@end

@implementation YFCommonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    UserGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView];
    
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    cell.item = item;
    if (indexPath.row == group.items.count - 1) {
        [cell showSeparationBottomLine:NO];
        [cell showSeparationLine:NO];
    }else {
        [cell showSeparationLine:YES];
    }
    if (indexPath.row == 0) {
        [cell showSeparationTopLine:NO];
    }else {
        [cell showSeparationTopLine:NO];
    }
    return cell;
}
// 选中的哪一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取出对应的模型数据
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    // 2.判断有没有block,有的话就执行
    if (item.block) {
        item.block(item,indexPath);
        return;
    }
    // 3.跳转
    if ([item isKindOfClass:[UserArrowItem class]]) {
        // 强转
        UserArrowItem *arrowItem = (UserArrowItem *)item;
        // 判断target是否有值
        if (arrowItem.target) {
            Class class = arrowItem.target;
            UIViewController *vc = [[class alloc] init];
            // 设置头部标题
            vc.title = item.title;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

#pragma mark 返回组不或者时尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return [self.groups[section] footerTitle];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UserGroup *group = self.groups[section];
    
    if (group.headerTitle != nil) {
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, group.headHeight)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kYFScreenWidth - 20, headView.bounds.size.height)];
        label.font = kOpenSansFont(OpenSansRegular, 14);
        label.textColor = kTLightGray999;
        
        label.text = group.headerTitle;
        [headView addSubview:label];
        headView.backgroundColor = kSeparationLineColor;
        
        return headView;
    }else {
        return [[UIView alloc] init];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRowHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    UserGroup *group = self.groups[section];
    if (group.headHeight <= 0) {
        return kSectionHeadViewHeight;
    }else {
        return group.headHeight;
    }
}

#pragma mark 懒加载
- (NSMutableArray <UserGroup *>*)groups {
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}


@end
